/*
 * SetLemming.java
 *
 * Created on 2007. mC!rcius 24., 12:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package laborhazi;
import java.util.ArrayList;

/**
 * When lemming collision occures this object stores all the lemmings that 
 * collided
 * @author nyuszik
 */
public class SetLemming extends FieldElement{
	private boolean updateSeq = true;
    private Field map;
    private int[] position;
    /** Creates a new instance of SetLemming */
    public SetLemming(Field field) {
    	setofLemmings = new ArrayList();
    	map = field;
    }
    private ArrayList setofLemmings;
    /** 
     * puts a lemming in when there are others on one field
     * @param lemming put this lemming in
     */
    public void add(FieldElement fieldElement) {
    	System.out.println("add " + fieldElement.toString());
    	setofLemmings.add(fieldElement);
    }
    /**
     * Sets the position
     * @param pos the position of the fieldelement
     */
    public void setPos(int[] poss) {
    	position = poss;
    }
    /** 
     * really good tool for getting a lemming from this set
     * @return a random lemming from this lemmingset
     */
    public Lemming getaLemming() {
    	return (Lemming)setofLemmings.get(0);
    }
    /** 
     * removes a lemming when there are others on one field
     * @param lemming remove this lemming
     */
    public void remove(FieldElement fieldElement) {
    	setofLemmings.remove(fieldElement);
    	setofLemmings.trimToSize();
    	if (setofLemmings.size() < 2) {
    		map.remove(this, position); // <- removes SetLemming
    		setofLemmings.trimToSize(); // <- do nothing?
    		// puts last lemming to field ->
    		map.put((FieldElement)setofLemmings.get(0), position); 
    		// update last lemming in setlemming
    		((FieldElement)setofLemmings.get(0)).update(updateSeq);
    		// removes last lemming from set lemming 
    		setofLemmings.remove(0);
    	}
    }
    /** 
     * updates all lemmings in the setlemming
     */
    public void update(boolean b) {
    	updateSeq = b;
    	Object[] lemi = new Object[setofLemmings.size()];
    	lemi = setofLemmings.toArray();
    	int k = setofLemmings.size();
    	for (int i = 0; i < k; i++) {
    		((Lemming)(lemi[i])).update(b); 
    	}
    }
    public String toString() {
    	String toWrite = new String();
    	toWrite = "SetLemming " + position[0] + " " + position[1] + " ";
    	Object[] lemi = new Object[setofLemmings.size()];
    	lemi = setofLemmings.toArray();
    	int k = setofLemmings.size();
    	for (int i = 0; i < k; i++) {
    		if (lemi[i] instanceof Lemming)
    			toWrite = toWrite + ((Lemming)(lemi[i])).toString();
    	}
    	return(toWrite);
    }
    public int getLemNum() {
    	return setofLemmings.size();
    }
    public Lemming getLem(int lemnum) {
    	return (Lemming)setofLemmings.get(lemnum);
    }
    /**
     * Blows all lemming in the setlemming
     */
     public void blow()	{
    	int k, i;
    	Object[] lemi = new Object[setofLemmings.size()];
     	lemi = setofLemmings.toArray();
     	k = setofLemmings.size();
     	for (i = 0; i < k; i++) {
     		((Lemming)(lemi[i])).blow();
     		setofLemmings.remove(lemi[i]);
     	}
     	map.remove(this, position);
     }
     public void goRock() {
    	int k, i;
     	Object[] lemi = new Object[setofLemmings.size()];
      	lemi = setofLemmings.toArray();
      	k = setofLemmings.size();
      	for (i = 0; i < k; i++) {
      		((Lemming)(lemi[i])).blow();
      		setofLemmings.remove(lemi[i]);
      	}
    	 map.remove(this, position);
    	 map.put(new Stone(map), position);
     }
     /**
      * Gives a lemming a tool
      */
     public boolean recieveTool(Tool tool) {
    	int k, i;
    	boolean recieved = false;
     	Object[] lemi = new Object[setofLemmings.size()];
      	lemi = setofLemmings.toArray();
      	k = setofLemmings.size();
      	for (i = 0; i < k && !recieved ; i++) {
      		if (((Lemming)(lemi[i])).recieveTool(tool)) {
      			recieved = true;
      		}
      	}
      	return recieved;
     }
     public void erase() {
    	
    	 for(int i = 1; i <= setofLemmings.size(); ++i){
    		 map.decLemming();
    	 }
    	 if(map.getLemmingNum() == 0) {
	    		map.viewer.game.end();
	    		}
    	 
    	 
     }
}
